package be.ac.ua.comp.projectGPT;

public class EnglishBiddingAgent extends BiddingAgent {
	private int mMaxBid;
	boolean mMoneyCheckedOut;
	protected EnglishAuctioneer mAuctioneer;
	
	EnglishBiddingAgent(int bid, Consumer consumer, EnglishAuctioneer auctioneer) {
		super(consumer);
		mMaxBid = bid;
		auctioneer.addBiddingAgent(this);
		mAuctioneer = auctioneer;
	}
	
	protected void finalize () {
		if (mMoneyCheckedOut) {
			mConsumer.depositMoney(mMaxBid);
		}		
    }
	
	public boolean bid(int bid) {
		if ((bid <= mMaxBid) && mConsumer.hasMoney(mMaxBid) && mConsumer.hasJob()) {
			mConsumer.withdrawMoney(mMaxBid);
			mChekedInJob = mConsumer.chekoutJob();
			mMoneyCheckedOut = true;
			return true;
		} else {
			return false; //don't bid (consumer is broke or has no jobs or bid is to high)
		}
	}
	
	public void setBid(int bid) {
		mMaxBid = bid;
	}

	public void loseBid() {
		mMoneyCheckedOut = false;
		mConsumer.depositMoney(mMaxBid);
		mConsumer.addJob(mChekedInJob);
		mChekedInJob = null;

	}
	
	public Job winBid() {
		mMoneyCheckedOut = false;
		Job job = mChekedInJob;
		mChekedInJob = null;
		return job;
	}
	
	public void withdraw() {
		mConsumer.removeBiddingAgent(this);
		mAuctioneer.removeBiddingAgent(this);
	}
}
